//
//  TestViewController.m
//  Cycling
//
//  Created by chijianguo on 2023/3/20.
//

#import "TestViewController.h"
#import "CycBLEManager.h"

@interface TestViewController ()
@property (weak, nonatomic) IBOutlet UILabel *valueLB;
@property (weak, nonatomic) IBOutlet UITextField *cmdTF;
@property (weak, nonatomic) IBOutlet UILabel *fcLB;
@property (weak, nonatomic) IBOutlet UILabel *outLB;

@end

@implementation TestViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"蓝牙测试";
    // Do any additional setup after loading the view from its nib.
    [CycBLEManager sharedManager].receiveDatafcBlock = ^(NSData *data) {
        self.fcLB.text = [self hexStringFromData:data];
    };
    [CycBLEManager sharedManager].receiveDataoutBlock = ^(NSData *data) {
        self.outLB.text = [self hexStringFromData:data];
//        [self paresData:data];
    };

}

// 解析数据
- (void)paresData:(NSData *)data {
    uint8_t cmd;
    [data getBytes:&cmd range:NSMakeRange(2, 1)];
    int8_t length;
    [data getBytes:&length range:NSMakeRange(3, 1)];
    NSData *value = [data subdataWithRange:NSMakeRange(4, length)];
    if(0xa0 == cmd){ // 车辆型号
        self.car.model = [self convertDataToHexStr:value];
        NSLog(@"A0******************: %@",self.car.model);
        self.outLB.text = [NSString stringWithFormat:@"%@\n车辆型号：%@",[self hexStringFromData:data],self.car.model];
    }
    if(0xa1 == cmd){ // 总里程
        NSString *hexStr = [self hexStringFromData:value];
        self.car.totalmileage = [hexStr integerValue]/10;
        NSLog(@"A1******************: %lu",(unsigned long)self.car.totalmileage);
        self.outLB.text = [NSString stringWithFormat:@"%@\n总里程：%lu",[self hexStringFromData:data],(unsigned long)self.car.totalmileage];
    }
    if(0xa2 == cmd){ // 单次里程
        NSString *hexStr = [self hexStringFromData:value];
        self.car.danci = [hexStr integerValue]/10;
        NSLog(@"A2******************: %lu",(unsigned long)self.car.danci);
        self.outLB.text = [NSString stringWithFormat:@"%@\n单词里程%lu",[self hexStringFromData:data],(unsigned long)self.car.danci];

    }
    if(0xa3 == cmd){ // 当前可骑行距离
        NSString *hexStr = [self hexStringFromData:value];
        self.car.canmileage = [hexStr integerValue]/10;
        NSLog(@"A3******************: %lu",(unsigned long)self.car.canmileage);
        self.outLB.text = [NSString stringWithFormat:@"%@\n可行驶距离%lu",[self hexStringFromData:data],(unsigned long)self.car.canmileage];

    }
    if(0xa5 == cmd){ // 当前档位
        NSString *hexStr = [self hexStringFromData:value];
        self.car.curr_dangwei = [hexStr integerValue];
        NSLog(@"A5******************: %lu",(unsigned long)self.car.curr_dangwei);
        self.outLB.text = [NSString stringWithFormat:@"%@\n档位%lu",[self hexStringFromData:data],(unsigned long)self.car.curr_dangwei];

    }
    if(0xa6 == cmd){ // 当前速度
        NSString *hexStr = [self hexStringFromData:value];
        self.car.curr_sudu = [hexStr integerValue]/10;
        NSLog(@"A6******************: %lu",(unsigned long)self.car.curr_sudu);
        self.outLB.text = [NSString stringWithFormat:@"%@\n速度%lu",[self hexStringFromData:data],(unsigned long)self.car.curr_sudu];

    }
    if(0xa7 == cmd){// 平均速度
        NSString *hexStr = [self hexStringFromData:value];
        self.car.avg_sudu = [hexStr integerValue]/10;
        NSLog(@"A7******************: %lu",(unsigned long)self.car.avg_sudu);
        self.outLB.text = [NSString stringWithFormat:@"%@\n平均速度%lu",[self hexStringFromData:data],(unsigned long)self.car.avg_sudu];

    }
    if(0xa8 == cmd){ // 最大速速
        NSString *hexStr = [self hexStringFromData:value];
        self.car.zuida_sudu = [hexStr integerValue]/10;
        NSLog(@"A8******************: %lu",(unsigned long)self.car.zuida_sudu);
        self.outLB.text = [NSString stringWithFormat:@"%@\n最大速度%lu",[self hexStringFromData:data],(unsigned long)self.car.zuida_sudu];

    }
    if(0xd2 == cmd){ // 仪表型号
        NSString *str = [self convertDataToHexStr:value];
        NSLog(@"D2******************: %@",str);
        self.outLB.text = [NSString stringWithFormat:@"%@\n仪表型号%@",[self hexStringFromData:data],str];
    }
    if(0xd3 == cmd){ // 仪表sn
        self.car.sn = [self convertDataToHexStr:value];
        NSLog(@"D3******************: %@",self.car.sn);
        self.outLB.text = [NSString stringWithFormat:@"%@\nsn%@",[self hexStringFromData:data],self.car.sn];
        
    }
    if(0xd5 == cmd){ // 蓝牙名称
        self.car.bleName = [self convertDataToHexStr:value];
        NSLog(@"D5******************: %@",self.car.bleName);
        self.outLB.text = [NSString stringWithFormat:@"%@\n蓝牙名称%@",[self hexStringFromData:data],self.car.bleName];

    }
    if(0xb3 == cmd){ // 电量
        NSString *hexStr = [self hexStringFromData:value];
        self.car.electricity = [hexStr integerValue];
        NSLog(@"B3******************: %lu",(unsigned long)self.car.electricity);
        self.outLB.text = [NSString stringWithFormat:@"%@\n电量%lu",[self hexStringFromData:data],(unsigned long)self.car.electricity];

    }
}

- (NSString *)convertDataToHexStr:(NSData *)data {
    NSLog(@"要转化的ascll码 =============== %@",data);
    NSString *str = @"";
    Byte *bytes = (Byte *) malloc(data.length);
    [data getBytes:bytes length:data.length];
    
    for (int i = 0; i < data.length; i ++) {
        char temp = bytes[i];
        str = [str stringByAppendingString:[NSString stringWithFormat:@"%c",temp]];
    }
    NSLog(@"ascll码转化的结果 ===============%@",str);
    
    return str;
    
}

//data转换为十六进制的string
-(NSString *)hexStringFromData:(NSData *)myD{
    
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    NSLog(@"hex = %@",hexStr);
    return hexStr;
}
//十六进制的string为转换data
- (NSData *) stringToHexData:(NSString *)hexStr
{
    int len = [hexStr length] / 2;    // Target length
    unsigned char *buf = malloc(len);
    unsigned char *whole_byte = buf;
    char byte_chars[3] = {'\0','\0','\0'};
    
    int i;
    for (i=0; i < [hexStr length] / 2; i++) {
        byte_chars[0] = [hexStr characterAtIndex:i*2];
        byte_chars[1] = [hexStr characterAtIndex:i*2+1];
        *whole_byte = strtol(byte_chars, NULL, 16);
        whole_byte++;
    }
    
    NSData *data = [NSData dataWithBytes:buf length:len];
    free( buf );
    return data;
}

- (IBAction)send:(id)sender {
    uint8_t cmd = (uint8_t)[self.cmdTF.text intValue];
    [self.car getCarMessageWithCmd:cmd retBlock:^(NSData * _Nonnull data, NSError * _Nonnull error) {
        if (data) {
            self.valueLB.text = [self hexStringFromData:data];
        }else {
            self.valueLB.text = @"消息发送失败";
        }
    }];
}

@end
